package com.baozun.houji.order.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.model.bean.ApiPagerResponse
import com.baozhun.mall.common.model.bean.OrderInfoBean
import com.baozun.houji.order.R
import com.baozun.houji.order.network.orderApiService
import com.blankj.utilcode.util.ToastUtils
import com.common.pay.PayParamsBean
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/20
 * @Version：1.0.0
 */
class OrderListViewModel : BaseViewModel() {

    private var mCurrentPage = 1 //当前页码

    var mOrderList = MutableLiveData<ApiPagerResponse<ArrayList<OrderInfoBean>>>()


    fun getOrderList(isRefresh: Boolean, orderStatus: Int?, search: String? = null) {
        if (isRefresh) mCurrentPage = 1
        request({ orderApiService.getOrderListData(orderStatus, search, mCurrentPage) }, {
            mOrderList.value = it
            mCurrentPage += 1
        }, isShowDialog = true)
    }

    /**
     * 售后订单列表
     */
    fun getAfterSaleList(isRefresh: Boolean) {
        if (isRefresh) mCurrentPage = 1
        request({ orderApiService.getAfterSaleList(mCurrentPage) }, {
            mOrderList.value = it
            mCurrentPage += 1
        }, isShowDialog = true)
    }

    /**
     * 取消訂單
     */
    fun cancelOrder(orderId: String, successCallback: () -> Unit) {
        request({ orderApiService.cancelOrder(orderId) }, {
            successCallback.invoke()
            ToastUtils.showShort(com.baozhun.mall.res.R.string.cancel_success)
        }, isShowDialog = true, showErrorMsg = true)
    }

    /**
     * 立即支付
     */
    fun orderPay(orderId: String, payType: String, successCallback: (payParamsBean: PayParamsBean) -> Unit) {
        request({ orderApiService.orderPay(orderId, payType) }, {
            successCallback.invoke(it)
        }, isShowDialog = true)
    }

    /**
     * 完成订单
     */
    fun finishOrder(orderId: String, successCallback: () -> Unit) {
        request({ orderApiService.finishOrder(orderId) }, {
            successCallback.invoke()
            ToastUtils.showShort(com.baozhun.mall.res.R.string.confirm_receive_success)
        }, isShowDialog = true)
    }

    /**
     * 删除订单
     */
    fun deleteOrder(orderId: String, successCallback: () -> Unit) {
        request({ orderApiService.deleteOrder(orderId) }, {
            successCallback.invoke()
            ToastUtils.showShort(com.baozhun.mall.res.R.string.delete_success)
        }, isShowDialog = true, showErrorMsg = true)
    }

    /**
     * 撤销申请
     */
    fun cancelRefund(afterSaleNo: String, withdrawSuccessCallBack: () -> Unit) {
        request({ orderApiService.cancelRefund(afterSaleNo) }, {
            withdrawSuccessCallBack.invoke()
            ToastUtils.showShort(com.baozhun.mall.res.R.string.withdraw_success)
        }, showErrorMsg = true)
    }
}